#include<list>
#include<iostream>
#include<ctime>
//int bumper_l1, bumper_l2, bumper_l3, bumper_l4, bumper_l5;

int led1Pin = 12, led2Pin = 11, led3Pin = 10, led4Pin = 9, led5Pin = 8;                // 定义LED 1 2 3 4 5 分别对应 12 11 10 9 8
//int switch1 = 1, switch2 = 2, switch3 = 3, switch4 = 4, switch5 = 5;                 // 定义传感器 bumperswitch 1 2 3 4 5 分别对应 1 2 3 4 5
int lighting_num = 0;
int bp1 = 0, bp2 = 0, bp3 = 0, bp4 = 0, bp5 = 0l;
int wrong = 0;
using namespace std;

//全局初始化l1~l5
Light l1;
Light l2;
Light l3;
Light l4;
Light l5;

class Light   //灯条模式   #######全部编写完后会用GUI的程序替换掉PASS 然后会写一个检测灯条mode的显示到GUI上面去###########
{
    //五个灯条基类
public:
    int num;
    int mode;
    void changemode(int md);
    void modedetect();
};

void Light::changemode(int md)
{
    mode = md;
}
void Light::modedetect()
{
    if (mode == 1)
    //暗状态
    _sleep(1);
    else if (mode == 2)
    //半亮状态
    _sleep(1);
    else if (mode == 3)
    //全亮状态
    _sleep(1);
    
}
void mid_motor()
{
    //pass
}

void bumper_detecting()  ////////////另threading
{
    while (true)
    {
        if (digitalRead(switch1) == HIGH)
            bp1 = 1;
        if (digitalRead(switch2) == HIGH)
            bp2 = 1;
        if (digitalRead(switch3) == HIGH)
            bp3 = 1;
        if (digitalRead(switch4) == HIGH)
            bp4 = 1;
        if (digitalRead(switch5) == HIGH)
            bp5 = 1;

    }


}


void wrongdetect()  ////////////另一线程一直while1运行wrongdetect 实现
{
    if (bp1 == 1 && lighting_num != 1)
        wrong = 1;
    if (bp2 == 1 && lighting_num != 2)
        wrong = 1;
    if (bp3 == 1 && lighting_num != 3)
        wrong = 1;
    if (bp4 == 1 && lighting_num != 4)
        wrong = 1;
    if (bp5 == 1 && lighting_num != 5)
        wrong = 1;

}


void correctdetect() ////////////另一线程运行correctdetect检测击打正确
{
    if (bp1 == 1 && lighting_num != 1)
        digitalWrite(led1Pin, HIGH); bp1 = 0;
    if (bp2 == 1 && lighting_num != 2)
        digitalWrite(led2Pin, HIGH); bp2 = 0;
    if (bp3 == 1 && lighting_num != 3)
        digitalWrite(led3Pin, HIGH); bp3 = 0;
    if (bp4 == 1 && lighting_num != 4)
        digitalWrite(led4Pin, HIGH); bp4 = 0;
    if (bp5 == 1 && lighting_num != 5)
        digitalWrite(led5Pin, HIGH); bp5 = 0;
}


void ltlnum_detect() //对随机数lighting_num检查后亮灯
{
    if (lighting_num == 1)
    {
        digitalWrite(led1Pin, HIGH);
        l1.changemode(3);
    }
    else if (lighting_num == 2)
    {
        digitalWrite(led2Pin, HIGH);
        l2.changemode(3);
    }
    else if (lighting_num == 3)
    {
        digitalWrite(led3Pin, HIGH);
        l3.changemode(3);
    }
    else if (lighting_num == 4)
    {
        digitalWrite(led4Pin, HIGH);
        l4.changemode(3);
    }
    else if (lighting_num == 5)
    {
        digitalWrite(led5Pin, HIGH);
        l5.changemode(3);
    }
}

void starting()
{   
    //reset:
    //while (digitalRead(switch1) == LOW && digitalRead(switch2) == LOW && digitalRead(switch3) == LOW && digitalRead(switch4) == LOW && digitalRead(switch5) == LOW)
    //
        digitalWrite(led1Pin, LOW);
        digitalWrite(led2Pin, LOW);
        digitalWrite(led3Pin, LOW);
        digitalWrite(led4Pin, LOW);
        digitalWrite(led5Pin, LOW);
        lighting_num = 1 + rand() % 5;    //取1~5随机数
        //cout << lighting_num << endl;   调试lighting_num
        if (lighting_num == 1)
        {
            digitalWrite(led1Pin, HIGH);
        }
        else if (lighting_num == 2)
        {
            digitalWrite(led2Pin, HIGH);
        }
        else if (lighting_num == 3)
        {
            digitalWrite(led3Pin, HIGH);
        }
        else if (lighting_num == 4)
        {
            digitalWrite(led4Pin, HIGH);
        }
        else if (lighting_num == 5)
        {
            digitalWrite(led5Pin, HIGH);
        }
        _sleep(2500);
        //goto reset;
    //}
}       
    /*
    std::list<int>::const_iterator cIter;
    std::list<int>lighting_num;
    lighting_num.clear();
    lighting_num = { 1,2,3,4,5 };
    std::cout << "list = ";
    for (auto& i : lighting_num)
        std::cout << " " << i;
    std::cout << std::endl;
    */
/*
void shooting()
{
    if (lighting_num == 1 && digitalRead(switch1) == HIGH)
    {
        //////////////
    }
    else if (lighting_num == 2 && digitalRead(switch2) == HIGH)
    {
        //////////////
    }
    else if (lighting_num == 3 && digitalRead(switch3) == HIGH)
    {
        //////////////
    }
    else if (lighting_num == 4 && digitalRead(switch4) == HIGH)
    {
        //////////////
    }
    else if (lighting_num == 5 && digitalRead(switch5) == HIGH)
    {
        //////////////
    }
    else
    {
        starting();
    }

}

*/
int setrdm(int range,int nownum) //函数功能  产生除了本数之外的随机数  range:产生1~range的随机数
{
    int rdm = 0;
    int rtnnum = nownum;
    while(rtnnum == nownum)
    {
        rdm = 1 + rand() % range;
        rtnnum = rdm;
    }
    return rtnnum;
}

int main()
{   
   
    //
    wrong = 0;
    while (1)
    {

    
        if (wrong == 0)
        {
        //while (bp1 == 0 && bp2 ==0 && bp3 == 0 && bp4 == 0 && bp5 ==0 )
            //{
        retry:
        starting();/////////////程序开始
        //_sleep(2500);
            //}
        }
        else if (wrong == 1)
        {
            goto retry;
        }
    }
////////////////////////////////////////////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!注意 需要分别同时运行 wrong_detect函数  correct_detect函数  bumper_detecting函数 main函数四个线程 达到效果
    
    //while (true)
    //{
    //int a = 0;
    //a = setrdm(5, 1);
    // std::cout << a << std::endl;
    //}
    //printf("%d", l1.mode);
    return 0;
    getchar();
}


